﻿using Yxinmt.Interface;
using Yxinmt.Utility;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Markup;
using System.Windows.Controls;
using Yxinmt.Controls;
using System.Threading;

namespace Yxinmt.Components
{
    public class PanelComponent : BaseComponent, IComponent
    {
        IControl _Panel { get; set; }


        public PanelComponent(Dictionary<string, object> property, IWindow form)
        {
            _Property = property;
            Window = form;
            this.InitSet();

            this.ControlList = new List<IControl>();
            _ControlDictionary = new Dictionary<Guid, List<System.Windows.FrameworkElement>>();

            string type = _Property.GetStringValue("type");

            if (type == "DockPanel") _Panel = new Controls.DockPanel(_Property, this);
            else if (type == "StackPanel") _Panel = new Controls.StackPanel(_Property, this);
            else if (type == "WrapPanel") _Panel = new Controls.WrapPanel(_Property, this);
            else if (type == "Grid") _Panel = new Controls.Grid(_Property, this);
            else if (type == "Canvas") _Panel = new Controls.Canvas(_Property, this);

            if (_Panel == null) throw new Exception($"未知组件类型：{type}");

            this.ControlList.Add(_Panel);

            LoadElements();
        }

        public override object GetValue()
        {
            return GetComponentsValue();
        }

        public override void SetValue(object value)
        {
            SetComponentsValue(value);
        }

        public void LoadElements()
        {
            ComponentList = new List<IComponent>();
            SetView();

            _Panel.Clear(); this.GetComponentsElements().ForEach(c => _Panel.Add(c));
        }

        public override bool GetVisible()
        {
            return _Panel.IsVisible;
        }

        public void SetBackgroundImage(string url)
        {
            _Panel.SetBackgroundImage(url);
        }

        public void SetBackgroundImage(System.Windows.Media.ImageSource imageSource)
        {
            _Panel.SetBackgroundImage(imageSource);
        }

        public double GetActualHeight()
        {
            return _Panel.Element.ActualHeight;
        }

        public double GetActualWidth()
        {
            return _Panel.Element.ActualWidth;
        }

        Dictionary<Guid, List<System.Windows.FrameworkElement>> _ControlDictionary { get; set; }

        public override IComponent Add(Dictionary<string, object> property)
        {
            IComponent component = this.AddProperty(property);
            List<System.Windows.FrameworkElement> elements = component.GetElements();
            _ControlDictionary.Add(property.GetValue<Guid>("id"), elements);

            elements.ForEach(c => _Panel.Add(c));

            component.EventLoad();
            component.DataLoad();

            return component;
        }

        public override void Remove(Dictionary<string, object> property)
        {
            Guid id = property.GetValue<Guid>("id");
            _PropertyList.Remove(property);
            IComponent component = ComponentList.Where(w => w.Id == id).FirstOrDefault();
            if (component != null) ComponentList.Remove(component);

            if (!_ControlDictionary.ContainsKey(id)) return;

            List<System.Windows.FrameworkElement> controlList = _ControlDictionary[id];

            controlList.ForEach(c => _Panel.Remove(c));
        }

        public override void SetBackColor(string color)
        {
            _Panel.SetBackColor(color);
        }
    }
}
